package com.irdstudio.efp.esb.api.rest.yx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.efp.esb.api.bean.yx.ReqPreOccQueryBean;
import com.irdstudio.efp.esb.api.bean.yx.RespPreQueryBean;
import com.irdstudio.efp.esb.api.common.EsbRespServiceBeanCreator;
import com.irdstudio.efp.esb.api.common.GroupeInterface;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.common.constant.EsbSysHeadEnums;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import com.irdstudio.efp.nls.service.facade.ed.LimitControlsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * 需求编号：【网贷已占用额度】
 * 开发人员：【chenxuan】
 * 创建/修改日期：【2019/8/13】
 * @Param
 * @return
 **/
@RestController
@RequestMapping("/esb")
public class PreOccQueryController {
	private static Logger logger = LoggerFactory.getLogger(PreOccQueryController.class);

	@PostMapping("/SERVICE_30230004_03")
	public EsbRespServiceBean doRecv(@Validated @ModelAttribute("esbReqServiceBean") EsbReqServiceBean esbReqServiceBean, BindingResult br1,
									 @Validated({GroupeInterface.bd.class}) @ModelAttribute("reqPreOccQueryBean") ReqPreOccQueryBean reqPreOccQueryBean, BindingResult br2, Exception exception) throws Exception {

		BigDecimal preOccAmt =BigDecimal.ZERO;
		try {
			// 通过切面校验进件申请参数是否正确，如不正确则返回错误信息
			if (Objects.nonNull(exception)) {
				throw exception;
			}
			 preOccAmt = limitControlsService.getPreOccAmt(reqPreOccQueryBean.getCertNo(), reqPreOccQueryBean.getCertTp());
			logger.info("证件号"+reqPreOccQueryBean.getCertNo()+"证件类型"+reqPreOccQueryBean.getCertTp()+"查询金额到已占用金额为"+preOccAmt.toString());
			return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean, //
						JSON.toJSONString(buildRespPreQueryBean(EsbBizEnums.RetCodeEnum.SUCCESS.getVALUE(), "查询成功", preOccAmt)),
						EsbSysHeadEnums.RetStEnum.SUCCESS.getCode(),
						EsbBizEnums.RetCodeEnum.SUCCESS.getVALUE(),
						"查询成功");
		} catch (BizException e) {
			logger.error("已占用额度接口业务处理失败!" + e.getMessage());
			e.printStackTrace();
			return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean, //
					JSON.toJSONString(buildRespPreQueryBean(EsbBizEnums.RetCodeEnum.SUCCESS.getVALUE(), "查询成功", preOccAmt)), //
					EsbSysHeadEnums.RetStEnum.FAILURE.getCode(),
					EsbBizEnums.RetCodeEnum.YWLJCLYC.getVALUE(),
					"查询失败");
		} catch (Exception e) {
			logger.error("已占用额度接口系统处理失败!" + e.getMessage());
			e.printStackTrace();
			return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean, //
					JSON.toJSONString(buildRespPreQueryBean(EsbBizEnums.RetCodeEnum.OTHERS_EXCEPTION.getVALUE(), e.getMessage(), preOccAmt)), //
					EsbSysHeadEnums.RetStEnum.FAILURE.getCode(), //
					EsbBizEnums.RetCodeEnum.OTHERS_EXCEPTION.getVALUE(), //
					e.getMessage());
		}
	}

	/**
	 * 需求编号：【已占用额度请求Bean】
	 * 问题编号：【暂无】
	 * 开发人员：【chenxuan】
	 * 创建/修改日期：【2019/8/13】
	 * 修改描述：
	 * @Param [retCd, retMsg, PreOccAmt]
	 * @return com.irdstudio.efp.esb.api.bean.yx.RespPreQueryBean
	 **/
	private RespPreQueryBean buildRespPreQueryBean( String retCd, String retMsg, BigDecimal PreOccAmt) throws BizException {
		RespPreQueryBean respPreQueryBean = new RespPreQueryBean();
		try {
			respPreQueryBean.setRetCd(retCd);
			respPreQueryBean.setRetMsg(retMsg);
			respPreQueryBean.setTkpLmt(PreOccAmt);//事务类型
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new BizException("数据异常");
		}
		return respPreQueryBean;
	}

	@Autowired
	@Qualifier("limitControlsService")
	private LimitControlsService limitControlsService;// 已占用额度

	@ModelAttribute("esbReqServiceBean")
	public EsbReqServiceBean getReqEsbHeadBean(HttpServletRequest request) {
		logger.info("校验 ESB请求系统头和请求应用头对象!");
		EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
		return esbReqServiceBean;
	}

	@ModelAttribute("reqPreOccQueryBean")
	public ReqPreOccQueryBean getReqEsbBodyBean(HttpServletRequest request) {
		logger.info("校验 ESB请求体对象！");
		String esbReqBody = (String) request.getAttribute("esbReqBody");
		return JSONObject.parseObject(esbReqBody, ReqPreOccQueryBean.class);
	}
}
